#include <bits/stdc++.h>
using namespace std;

int main() {
    string s = "Hello? World? This? is? a? string? with? question? marks?";
    char targetChar = '?'; // 要查找和替换的字符
    char replaceChar = '!'; // 要插入的字符
    // 从后往前查找
    size_t pos = s.rfind('?');
    while(pos != string::npos) {
        s.replace(pos, 1, 2, replaceChar);
        if(pos > 0) {
            pos = s.rfind(targetChar, pos-1);
        }else {
            break;
        }
    }
    cout << "Modified string: " << s << endl;
//     size_t pos2 = s.find('?');
//     while(pos2 != string::npos) {
//         s.replace(pos2, 1, 1, replaceChar);
//         pos2 = s.find(targetChar, pos2+1);
//     }
//     cout << "Modified string: " << s << endl;

    string s1 = "hello world";
    string s2 = "beautiful";
    size_t p = 6;
    s1.replace(p, 0, s2); // 不删除任何字符的情况下 在指定的位置插入子字符串
    cout << s1 << endl;

    string s3 = "really";
    size_t p2 = 0;
    s3.replace(p2, 4, s2.substr(1, 3));
    cout << s3 << endl;
}